#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define in read()
#define pii pair<int,int>
#define fi first
#define se second
#define FILE(x) freopen(x".in","r",stdin);\
	freopen(x".out","w",stdout);
#define pb push_back

int read(){
	int x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
	return x*sgn;
}

const int LIM = 5e6+10;

ll n,s[LIM];
int p,prime[LIM],pnum,phi[LIM],inv6;
bool v[LIM];
unordered_map<ll,ll> hstb;

void init(){
	phi[1] = 1;
	for(int i = 2;i < LIM;i++){
		if(!v[i]) {prime[++pnum] = i; phi[i] = i-1;}
		for(int j = 1,x;j <= pnum && prime[j] * i < LIM;j++){
			v[x = prime[j] * i] = 1; if(i % prime[j] == 0) {phi[x] = 1ll * phi[i] * prime[j] % p;break;}
			phi[x] = 1ll * phi[i] * phi[prime[j]] % p;
		}
	}for(int i = 1;i < LIM;i++) s[i] = (s[i-1] + 1ll * phi[i] * i % p * i % p) % p;
}

ll sq(ll x){x %= p;return x * x % p;}

ll S(ll x){x %= p;return x * (x+1) / 2 % p;}

ll Sum(ll x){x %= p;return x * (x+1) % p * (2*x+1) % p * inv6 % p;}

ll gets(ll x){
	if(x < LIM) return s[x]; if(hstb[x]) return hstb[x];
	ll ans = sq(S(x)) % p;
	for(ll l = 2,r;l <= x;l=r+1){
		r = min(x/(x/l),x);
		ans = (ans + p - (Sum(r) - Sum(l-1)) * gets(x/l) % p) % p;
	}ans = (ans % p + p) % p;return hstb[x] = ans;
}

ll solve(ll n){
	ll ans = 0;
	for(ll l = 1,r;l <= n;l=r+1){
		r = min(n/(n/l),n);
		ans = (ans + sq(S(n/l)) % p * (gets(r) - gets(l-1)) % p) % p;
	}ans = (ans + p) % p;return ans;
}

ll qp(ll x,int t){ll res=1;for(;t;t>>=1,x=x*x%p)if(t&1) res=res*x%p;return res;}

int main (){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	scanf("%d %lld",&p,&n);
	init(); inv6 = qp(6,p-2);
	printf("%lld\n",solve(n));
	return 0;
}

